import Vue from 'vue';
import utils from './utils';
import md5 from '../libs/md5.min.js';

const http = {
	// 基本地址
	baseUrl: function() {
		return "http://localhost:8080/coconut";
	},
	// 存储的名称token信息名称
	authInfoName: function() {
		return "coconut_auth_info";
	},
	// 获取 token
	getToken: function() {
		let userInfo = uni.getStorageSync(http.authInfoName());
		return userInfo && userInfo.token || "";
	},
	// 获取请求头信息
	headers: function(url) {
		let headers = {};
		if (url.startsWith('/cauth')) {
			// 用于非常登录认证的用户访问地址
			headers["appKey"] = "coconut_app_client_uyhloh";
			headers["timespan"] = parseInt(new Date().getTime() / 1000 + "");
			let appSecret = "coconut_app_secret_qfceta";
			headers["token"] = md5(headers["appKey"] + headers["timespan"] + appSecret);
		} else {
			headers["token"] = http.getToken();
		}
		return headers;
	},
	// 删除 token
	clearToken: function() {
		uni.removeStorageSync(http.authInfoName());
	},
	// post 请求
	post: function(url, data, success, stip, etip, loading, error) {
		loading && utils.showLoading()
		uni.request({
			url: http.baseUrl() + url,
			data: data,
			header: http.headers(url),
			method: 'POST',
			dataType: 'json',
			responseType: 'text',
			success: function(res) {
				let resData = (res && res.data) || {};
				if (resData.code == "success") {
					stip && utils.toast(stip===true? "操作成功！":stip, 2000, "success");
					success && success(resData);
				} else if (resData.code == "login") {
					// 清除无效的token数据
					http.clearToken();
					utils.modal("提示", "请先登录！", true, (flag) => {
						if (flag) {
							uni.redirectTo({
								url: '/pages/login/login'
							});
						}
					});
				} else {
					etip && utils.modal('错误', resData.msg || '请求错误', false, null, null, "关闭");
					error && error(resData);
				}
			},
			fail: function(res) {
				etip && utils.toast();
			},
			complete: function(res) {
				loading && utils.hideLoading();
				uni.stopPullDownRefresh();
			}
		})
	},
	// post 请求
	get: function(url, data, success, stip, etip, loading, error) {
		loading && utils.showLoading()
		uni.request({
			url: http.baseUrl() + url,
			data: data,
			header: http.headers(url),
			method: 'GET',
			dataType: 'json',
			responseType: 'text',
			success: function(res) {
				let resData = (res && res.data) || {};
				if (resData.code == "success") {
					stip && utils.toast(stip===true?"操作成功！":stip, 2000, "success");
					success && success(resData);
				} else if (resData.code == "login") {
					// 清除无效的token数据
					http.clearToken();
					utils.modal("提示", "请先登录！", true, (flag) => {
						if (flag) {
							uni.redirectTo({
								url: '/pages/login/login'
							});
						}
					});
				} else {
					etip && utils.modal('错误', resData.msg || '请求错误', false, null, null, "关闭");
					error && error(resData);
				}
			},
			fail: function(res) {
				etip && utils.toast();
			},
			complete: function(res) {
				loading && utils.hideLoading();
				uni.stopPullDownRefresh();
			},
		})
	},
}


Vue.prototype.$http = http;

export default http;